<template>
  <div class="org-tree-node">
    <!-- 节点内容 -->
    <div class="node-content">
      <div class="node-details">
        <!-- 职位（不可编辑，居中） -->
        <div class="node-position">{{ node.position }}</div>
        <!-- 姓名（可编辑，居中） -->
        <div class="node-name">
          <input
              v-model="editableName"
              @blur="saveName"
              class="name-input"
              type="text"
          />
        </div>
      </div>
      <div class="buttons">
        <button class="btn btn-add" @click="showAddForm = true">添加</button>
        <button class="btn btn-delete" @click="deleteSelf" v-if="hasParent">删除</button>
      </div>
    </div>

    <!-- 添加新节点的表单 -->
    <div v-if="showAddForm" class="add-form">
      <input v-model="newPosition" placeholder="职位" class="form-input" />
      <select v-model="newName" class="form-input">
        <option disabled value="">请选择姓名</option>
        <option value="李红">李红</option>
        <option value="张杰">张杰</option>
        <option value="王娟">王娟</option>
      </select>
      <div class="button-group">
      <button class="btn btn-confirm" @click="confirmAdd">确认</button>
      <button class="btn btn-cancel" @click="cancelAdd">取消</button>
      </div>
    </div>

    <!-- 连接线 -->
    <div v-if="node.children.length" class="branch-line"></div>

    <!-- 子节点 -->
    <div v-if="node.children.length" class="children">
      <tree-node
          v-for="(child, index) in node.children"
          :key="child.id"
          :node="child"
          :parent-node="node"
          :node-index="index"
          @add-node="$emit('add-node', $event)"
          @delete-node="$emit('delete-node', $event)"
          @edit-node="$emit('edit-node', $event)"
      />
    </div>
  </div>
</template>

<script>
export default {
  name: 'TreeNode',
  props: {
    node: {
      type: Object,
      required: true,
    },
    parentNode: Object,
    nodeIndex: Number,
  },
  data() {
    return {
      editableName: this.node.name,
      showAddForm: false,
      newPosition: '',
      newName: '', // 用于下拉框的选择
    };
  },
  computed: {
    hasParent() {
      return !!this.parentNode;
    },
  },
  methods: {
    confirmAdd() {
      if (this.newPosition && this.newName) {
        const newNode = {
          id: Date.now(),
          position: this.newPosition,
          name: this.newName,
          children: [],
        };
        this.node.children.push(newNode);
        this.resetForm();
      } else {
        alert('请填写职位和选择姓名');
      }
    },
    cancelAdd() {
      this.resetForm();
    },
    resetForm() {
      this.newPosition = '';
      this.newName = '';
      this.showAddForm = false;
    },
    deleteSelf() {
      this.$emit('delete-node', this.parentNode, this.nodeIndex);
    },
    saveName() {
      this.node.name = this.editableName;
      this.$emit('edit-node', this.node);
    },
  },
};
</script>

<style scoped>
/* 树节点容器 */
.org-tree-node {
  display: flex;
  flex-direction: column;
  align-items: center;
  position: relative;
  margin-bottom: 40px;
}

/* 节点内容 */
.node-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-bottom: 10px;
  background-color: #f0f4f8;
  padding: 1.5vw;
  border-radius: 8px;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
  border: 1px solid #ccd6dd;
  text-align: center;
  max-width: 480px;
}

/* 职位（字体较大，居中） */
.node-position {
  font-size: 1.6rem;
  font-weight: bold;
  color: #0074d9;
  margin-bottom: 5px;
  text-align: center;
  white-space: nowrap;
}

/* 姓名（字体较大，居中） */
.node-name {
  font-size: 1.3rem;
  color: #333;
  text-align: center;
}

.name-input {
  font-size: 1.3rem;
  color: #333;
  border: none;
  background: transparent;
  text-align: center;
  width: 120px;
  border-bottom: 1px solid #0074d9;
  outline: none;
}

/* 按钮样式 */
.buttons {
  margin-top: 8px;
}

.btn {
  background-color: #0074d9;
  color: #fff;
  border: none;
  padding: 0.8rem 1rem;
  border-radius: 4px;
  cursor: pointer;
  font-size: 1rem;
  margin-right: 5px;
  transition: background-color 0.3s ease;
}

.btn:hover {
  background-color: #0056a3;
}
.confirm-button.orange {
  background-color: 	#FFDD55; /* 橙色 */
}

.confirm-button.orange:hover {
  background-color: 	#FFDD55; /* 深橙色 */
}
/* 添加按钮 */
.btn-add {
  background-color: 	#FF8800; /* 橙色 */
}

.btn-add:hover {
  background-color: 	#FF8800; /* 深橙色 */
}

/* 删除按钮 */
.btn-delete {
  background-color: #95a5a6;
}

.btn-delete:hover {
  background-color: #7f8c8d;
}

/* 确认按钮 */
.btn-confirm {
  background-color: #3498db;
}

.btn-confirm:hover {
  background-color: #2980b9;
}

/* 取消按钮 */
.btn-cancel {
  background-color: #95a5a6;
}

.btn-cancel:hover {
  background-color: #7f8c8d;
}
/* 表单容器 */
.add-form {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-top: 10px;
}

/* 输入框和下拉框 */
.form-input {
  width: 100%;
  max-width: 300px; /* 调整宽度 */
  padding: 10px; /* 增加填充 */
  font-size: 1.2rem; /* 增大字体 */
  margin-bottom: 15px; /* 增加输入框之间的间距 */
  border: 1px solid #ccd6dd;
  border-radius: 5px;
  box-sizing: border-box;
}

/* 按钮容器 */
.button-group {
  display: flex;
  justify-content: space-between;
  width: 100%;
  max-width: 300px; /* 调整按钮组的宽度 */
  margin-top: 10px;
}

/* 确认按钮和取消按钮 */
.btn-confirm,
.btn-cancel {
  flex: 0.5;
  padding: 10px;
  font-size: 1.2rem; /* 增大按钮字体 */
  margin: 0 5px; /* 按钮之间的间距 */
  border-radius: 5px;
}

.btn-confirm {
  background-color: #3498db;
}

.btn-confirm:hover {
  background-color: #2980b9;
}

.btn-cancel {
  background-color: #95a5a6;
}

.btn-cancel:hover {
  background-color: #7f8c8d;
}


/* 子节点样式 */
.children {
  display: flex;
  justify-content: center;
  margin-top: 10px;
  position: relative;
  width: 100%;
}

/* 父节点到子节点的垂直连接线 */
.branch-line {
  width: 2px;
  height: 30px;
  background-color: #0074d9;
  position: absolute;
  top: -10px;
  left: 50%;
}

/* 父节点的子节点的横向连接线（从父节点到所有子节点） */
.children::before {
  content: '';
  position: absolute;
  top: -10px;
  left: 0;
  right: 0;
  height: 2px;
  background-color: #0074d9;
  width: 100%; /* 横线覆盖全部子节点 */
}

/* 每个子节点的垂直连接线 */
.children > .org-tree-node {
  margin: 0 20px;
  position: relative;
}

.children > .org-tree-node::before {
  content: '';
  position: absolute;
  top: -10px;
  width: 2px;
  height: 10px;
  background-color: #0074d9;
  left: 50%;
}
</style>





